package com.example.scaloovka;

import java.math.RoundingMode;
import java.text.DecimalFormat;

public class Calculator {
	
	public static String[] calculate(double scale, double value, String unit){
		
		double[] scalesMap = ScalesMap.getInstance().getScales();
		
		double[] outValues = new double[scalesMap.length];
        double oneToOne = value * scale;
        DecimalFormat df = new DecimalFormat("#.####");
        df.setRoundingMode(RoundingMode.HALF_UP);
        for(int i=0; i<scalesMap.length; i++)
        {
            outValues[i] = oneToOne / scalesMap[i];
        }
        
        String[] result = new String[outValues.length];
        for (int i = 0; i < outValues.length; i++)
        {
            String s = df.format(outValues[i]) + unit;
            switch (unit)
            {
                case "mm" :
                    if (outValues[i] >= 10.0)
                        s = df.format(outValues[i] / 10.0) + "cm";
                    else if (outValues[i] >= 1000.0)
                        s = df.format(outValues[i] / 1000.0) + "m";
                    else if (outValues[i] >= 1000000.0)
                        s = df.format(outValues[i] / 1000000.0) + "km";
                    break;
                case "cm":
                    if (outValues[i] < 1.0)
                        s = df.format(outValues[i] * 10.0) + "mm";
                    else if (outValues[i] >= 100.0)
                        s = df.format(outValues[i] / 100.0) + "m";
                    else if (outValues[i] >= 100000.0)
                        s = df.format(outValues[i] / 100000.0) + "km";
                    break;
                case "m":
                    if (outValues[i] < 0.01)
                        s = df.format(outValues[i] * 1000.0) + "mm";
                    else if (outValues[i] <= 1.0)
                        s = df.format(outValues[i] * 100.0) + "cm";
                    else if (outValues[i] >= 1000.0)
                        s = df.format(outValues[i] / 1000.0) + "km";
                    break;
                case "km":
                    if (outValues[i] < 0.0001)
                        s = df.format(outValues[i] * 1000000.0) + "mm";
                    else if (outValues[i] < 0.001)
                        s = df.format(outValues[i] * 100000.0) + "cm";
                    else if (outValues[i] < 1.0)
                        s = df.format(outValues[i] * 1000.0) + "m";
                    break;
                default :
                    s = df.format(outValues[i]) + unit;
                    break;
                    
            }
            result[i] = s;
        }
		return result;
	}

}
